package jgo;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RouterPlugin extends Plugin {
	private static final Logger logger = LoggerFactory.getLogger(RouterPlugin.class);
	public List<Route> routes = new ArrayList<>();

	@Override
	public void init(JGo jgo) {
		super.init(jgo);
		logger.info("Initializing RouterPlugin");
	}

	public RouterPlugin addRoute(String method, String path, Handler handler) {
		routes.add(new Route(method, path, handler));
		logger.debug("Added route: {} {}", method, path);
		return this;
	}

	public Route findRoute(String method, String path) {
		logger.debug("Finding route for {} {}", method, path);
		for (Route route : routes) {
			if (route.matches(method, path)) {
				return route;
			}
		}
		return null;
	}

	public RouterPlugin get(String path, Handler handler) {
		return addRoute(Http.GET, path, handler);
	}

	public RouterPlugin post(String path, Handler handler) {
		return addRoute(Http.POST, path, handler);
	}

	public RouterPlugin put(String path, Handler handler) {
		return addRoute(Http.PUT, path, handler);
	}

	public RouterPlugin delete(String path, Handler handler) {
		return addRoute(Http.DELETE, path, handler);
	}

	// 可以根据需要添加其他 HTTP 方法
}